﻿<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<link rel="stylesheet" type="text/css" href="../themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="../themes/icon.css">
	<link rel="stylesheet" type="text/css" href="../demo.css">
	<script type="text/javascript" src="../jquery.min.js"></script>
	<script type="text/javascript" src="../jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="../extend.js"></script>
	<script type="text/javascript" src="../jquery.cookie.js"></script>
    <script>
        var dataListCache = null;
        var dataCache = null;
		var searchArg = [];
        var appId = GetQueryString("moduleid");
        var titles = getTitle();
        var departments = getDepartment();
        var users = getUser();
        var banks = getBank();
        window.onresize=calSize;

        $(document).ready(function(){
            initBaseInfo();
            setData();
            calSize();
		});
        function initBaseInfo(setDataFunction){
			$('#UserProjection').combogrid(getComboGridInfo('对应用户','Name',[[
                            {field:'AutoId',title:'ID',width:80},
                            {field:'Code',title:'代号',width:120},
                            {field:'Name',title:'名称',width:80,align:'right'},
                        ]],users));
			$('#TitleId').combogrid(getComboGridInfo('职等','Name',[[
                            {field:'AutoId',title:'ID',width:80},
                            {field:'Code',title:'代号',width:120},
                            {field:'Name',title:'名称',width:80,align:'right'},
                        ]],titles));
			$('#DepartmentId').combogrid(getComboGridInfo('所属部门','Name',[[
                            {field:'AutoId',title:'ID',width:80},
                            {field:'Code',title:'代号',width:120},
                            {field:'Name',title:'名称',width:80,align:'right'},
                        ]],departments));
            $('#BankId').combobox({data:banks});
        }
        function pageChanging(pageNumber, pageSize) {
            showMask("查找中");
            $.get('../../api/employee/list', { Args: searchArg, Length: pageSize, Pos: (pageNumber-1) * pageSize }, function (data) {
                if (data.Result == 0) {
                    dataListCache = JSON.parse(data.Data);
                    $("#employeeList").datagrid("loadData", dataListCache);
                    $("#employeeList").datagrid('getPager').pagination({ total: data.Count, pageNumber: pageNumber, onSelectPage: pageChanging });
                } else {
                    $.messager.alert('提示', data.Message, 'error');
                }
                hideMask();
            }, 'json');
        }
        function setData() {
            pageChanging(1,10);
        }
        window.onresize=calSize;
        function calSize(){
            var height=window.parent.document.getElementById("mainFrame").clientHeight;
            var width=window.parent.document.getElementById("mainFrame").clientWidth;
            window.parent.document.getElementById(appId).height = height-32;
            $("#employeeList").datagrid('resize',{height:height-76,width:width-15});
            $("#toolbar").panel({width:width-15});
            $("#editToolbar").panel({width:width-15});
            $("#editZone").panel('resize',{height:height-76,width:width-15});
        }
        function initVal(){
            $('#AutoId').val(dataCache.AutoId);
            $('#Flag').val(dataCache.Flag);
            $('#Site').val(dataCache.Site);
			$('#State').val(dataCache.State);
            $('#Code').textbox("setValue",dataCache.Code);
            $('#Name').textbox("setValue",dataCache.Name);
            $('#Tel').textbox("setValue",dataCache.Tel);
            $('#Sex').combobox("setValue",dataCache.Sex);
            $('#DepartmentId').combogrid("setValue",dataCache.DepartmentId);
            $('#UserProjection').combogrid("setValue",dataCache.UserProjection);
            $('#TitleId').combogrid("setValue",dataCache.TitleId);
            $('#HireDate').datebox("setValue",dataCache.HireDate);
            $('#BankId').combobox("setValue",dataCache.BankId);
            $('#Account').textbox("setValue",dataCache.Account);
            $('#Define1').textbox("setValue",dataCache.Define1);

        }
		function deleteRecord(){
			if (dataCache == null||dataCache.AutoId==0) {
                $.messager.alert("错误", "请先选中要删除的对象", "error");
                return;
            }
			$.messager.confirm('删除提示', '确定删除该记录?', function(r){
				if (r){
					showMask();
					$.post("../../api/employee/remove",dataCache,function(response){
						if(response.Result==0){
							for(var i=0;i<dataListCache.length;i++){
								if(dataListCache[i].AutoId == dataCache.AutoId){
									dataListCache.splice(i,1);
									break;
								}
							}					
							dataCache = null;
							$('#employeeList').datagrid('loadData', dataListCache); 
							$.messager.alert("信息","操作成功","info");
						}else{
							$.messager.alert("错误",response.Message,"error");
						}
					})
					.error(function(){
						$.messager.alert("错误","系统内部错误","error");
					})
					.complete(function() { 
						hideMask();
					});	
				}
			});		
		}
        function createNewRecord(){
            dataCache = new Object();
            initVal();
            $('#editContent').window('open');
        }
		function selectRecord(rowIndex,rowData){
			dataCache = rowData;
		}
        function editRecord(rowIndex,rowData){
            dataCache = rowData;
            initVal();
            $('#editContent').window('open');
        }
        function submitChange(){
            if (dataCache == null) {
                $.messager.alert("错误", "无法获取被编辑的对象", "error");
                return;
            }
            showMask();
			var oldData = JSON.parse(JSON.stringify(dataCache));
			dataCache.State = $("State").val();
            dataCache.Flag = $("#Flag").val();
            dataCache.Code=$("#Code").textbox("getValue");
            dataCache.Name=$("#Name").textbox("getValue");
            dataCache.Tel=$("#Tel").textbox("getValue");
            dataCache.Sex=$("#Sex").combobox("getValue");
            dataCache.HireDate=$("#HireDate").datebox("getValue");
            dataCache.DepartmentId=$("#DepartmentId").combogrid("getValue");
            dataCache.UserProjection=$("#UserProjection").combogrid("getValue");
            dataCache.TitleId=$("#TitleId").combogrid("getValue");
            dataCache.BankId = $("#BankId").combobox("getValue");
            dataCache.Account = $("#Account").textbox("getValue");
            dataCache.Define1 = $("#Define1").textbox("getValue");
            $.post("../../api/employee/set",dataCache,function(response){
                if(response.Result==0){
                    dataCache = JSON.parse(response.Data);
                    if(dataCache.Flag != 1){
                        for(var i=0;i<dataListCache.length;i++){
                            if(dataListCache[i].AutoId == dataCache.AutoId){
                                dataListCache.splice(i,1,dataCache);
								console.log('列表第'+i+'项被更新');
                                break;
                            }
                        }
                    }else{
						dataListCache.splice(0,0,dataCache);
						console.log('新项被追加到列表');
					}
                    $('#employeeList').datagrid('loadData', dataListCache); 
                    $.messager.alert("信息","操作成功","info");
                    $('#editContent').window('close');
                }else{
                    $.messager.alert("错误",response.Message,"error");
					dataCache = oldData;
                }
            })
            .error(function(){
                $.messager.alert("错误","系统内部错误","error");
				dataCache = oldData;
            })
            .complete(function() { 
                hideMask();
            });
        }
        function boolFormatter(value,row,index){
            if(value){
                return "是";
            }else{
                return "否";
            }
        }
        function sexFormatter(value,row,index){
            if(value==2){
                return "女";
            }else if(value==1){
                return "男";
            }else{
                return "未知";
            }
        }
        function titleFormatter(value,row,index){
			return valToName(titles,value);
        }
        function userFormatter(value,row,index){
			return valToName(users,value);
        }
        function depFormatter(value,row,index){
			return valToName(departments,value);
        }
		function bankCodeFormatter(value,row,index){
			for(var i=0;i<banks.length;i++){
				if(banks[i].AutoId == row['BankId']){
					return banks[i].Code;
				}
			}
		}
		function bankNameFormatter(value,row,index){
			for(var i=0;i<banks.length;i++){
				if(banks[i].AutoId == row['BankId']){
					return banks[i].Name;
				}
			}
		}
		function bankDescFormatter(value,row,index){
			for(var i=0;i<banks.length;i++){
				if(banks[i].AutoId == row['BankId']){
					return banks[i].BankDesc;
				}
			}
		}
		function bankSelected(n,o){
			$('#BankId_Code').textbox('setValue','');
			$('#BankId_Desc').textbox('setValue','');
			for(var i=0;i<banks.length;i++){
				if(banks[i].AutoId == n){
					$('#BankId_Code').textbox('setValue',banks[i].Code);
					$('#BankId_Desc').textbox('setValue',banks[i].BankDesc);
					return;
				}
			}
		}
    </script>
<head>
<body onload = "hideMask()">
    <script>showMask('正在载入数据');</script>
	<div id="toolbar" class="easyui-panel" style="padding:1px;">
		<a href="#" class="easyui-linkbutton" data-options="onClick:createNewRecord,iconCls:'icon-edit',plain:true">新建</a>
		<a href="#" class="easyui-linkbutton" data-options="onClick:deleteRecord,iconCls:'icon-remove',plain:true">删除</a>
        <a href="#" class="easyui-linkbutton" data-options="onClick:function(){$('#search').window('open')},iconCls:'icon-search',plain:true">搜索</a>
	</div>
    <div style="margin:2px"></div>
    <table id="employeeList" class="easyui-datagrid"
        data-options="singleSelect:true,method:'get',onClickRow:selectRecord,onDblClickRow:editRecord,pagination:true,striped:true, pagination:true,rownumbers: true,">
        <thead>
            <tr>
                <th data-options="field:'AutoId',hidden:true"></th>
                <th data-options="field:'Flag',hidden:true"></th>
                <th data-options="field:'State',hidden:true"></th>
                <th data-options="field:'Code',width:100,align:'right'">代号</th>
                <th data-options="field:'Name',width:100,align:'right'">姓名</th>
                <th data-options="field:'Sex',width:80,align:'right',formatter:sexFormatter">性别</th>
                <th data-options="field:'Tel',width:120,align:'right'">电话</th>
                <th data-options="field:'DepartmentId',width:120,align:'right',formatter:depFormatter">部门</th>
                <th data-options="field:'UserProjection',width:100,align:'right',formatter:userFormatter">登录名</th>
                <th data-options="field:'TitleId',width:120,align:'right',formatter:titleFormatter">职等</th>
                <th data-options="field:'HireDate',width:120,align:'right'">入职日期</th>
			 <th data-options="field:'BankId',width:150,align:'right',formatter:bankCodeFormatter">分行代号</th>
			 <th data-options="field:'BankId_Name',width:150,align:'right',formatter:bankNameFormatter">分行名称</th>
			 <th data-options="field:'BankId_BankDesc',width:120,align:'right',formatter:bankDescFormatter">所属银行名称</th>
                <th data-options="field:'Account',width:150,align:'right'">银行账号</th>
                <th data-options="field:'Define1',width:120,align:'right'">设计公司</th>
            </tr>
        </thead>
    </table>
    <div id="editContent" class="easyui-window" title="编辑框" data-options="noheader:true,maximized:true,modal:true,closed:true,iconCls:'icon-save',border:false" style="padding:5px 5px 0px 5px">
        <div class="easyui-panel" id="editToolbar" style="padding:1px;width:100%">
            <a href="#" class="easyui-linkbutton" data-options="onClick:submitChange,iconCls:'icon-save',plain:true">保存</a>
            <a href="#" class="easyui-linkbutton" data-options="onClick:initVal,iconCls:'icon-no',plain:true">取消</a>
            <a href="#" class="easyui-linkbutton" data-options="onClick:function(){$('#editContent').window('close');},iconCls:'icon-blank',plain:true">关闭</a>
        </div>
        <div style="margin:2px"></div>
        <div id="editZone" class="easyui-panel" style="padding:4px 4px 0px 4px" >
            <table>
                <tr>
                    <td><input class="easyui-textbox" id="Code"  style="width:300px" data-options="label:'代号:',required:true"></td>
                    <td>&nbsp;</td>
                    <td><input class="easyui-textbox" id="Name"  style="width:300px" data-options="label:'姓名:'"></td>
					<td>&nbsp;</td>
                    <td><input class="easyui-combobox" id="Sex"  style="width:300px" data-options="label:'性别:',valueField: 'id',textField: 'name',data:[{id:0,name:'未知'},{id:1,name:'男'},{id:2,name:'女'}]"></td>
                </tr>
                <tr>
                    <td><input class="easyui-textbox" id="Tel"  style="width:300px" data-options="label:'电话:'"></td>
					<td>&nbsp;</td>
					<td><input class="easyui-combogrid" id="DepartmentId"  style="width:300px" ></td>
                    <td>&nbsp;</td>
                    <td><input class="easyui-combogrid" id="TitleId"  style="width:300px" ></td>
                </tr>
                <tr>
                    <td><input class="easyui-combogrid" id="UserProjection"  style="width:300px"></td>
                    <td>&nbsp;</td>
                    <td><input class="easyui-datebox" id="HireDate"  style="width:300px" data-options="label:'入职日期:',formatter:dateFormatter,parser:dateParser"></td>
					<td>&nbsp;</td>
					<td><input class="easyui-combobox" id="BankId"  style="width:300px" data-options="label:'所属银行分行:',valueField: 'AutoId',textField: 'Name',onChange:bankSelected"></td>
				</tr>
                <tr>
                    <td><input class="easyui-textbox" id="BankId_Code"  style="width:300px" data-options="label:'分行代号:',editable:false"></td>
					<td>&nbsp;</td>
                    <td><input class="easyui-textbox" id="BankId_Desc"  style="width:300px" data-options="label:'所属银行:'"></td>
                    <td>&nbsp;</td>
                    <td><input class="easyui-textbox" id="Account"  style="width:300px" data-options="label:'银行账号:'"></td>
                </tr>
                <tr>
                    <td><input class="easyui-textbox" id="Define1"  style="width:300px" data-options="label:'设计公司:'"></td>
					<td></td>
					<td></td>
                    <td>&nbsp;</td>
                    <td></td>
                </tr>
            </table>
            <div hidden>
                <p id="AutoId"></p>
                <p id="Flag"></p>
                <p id="Site"></p>
				<p id="State"></p>
            </div>
			<div id="search" class="easyui-window" title="搜索框" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:500px;height:400px;padding:10px;">
                <div style="margin-bottom:5px">
                    <input class="easyui-textbox" id="sch_Code" style="width:100%" data-options="label:'工号:'">
                </div>
                <div style="margin-bottom:5px">
                    <input class="easyui-textbox" id="sch_Name" style="width:100%" data-options="label:'姓名:'">
                </div>
                <div style="margin-bottom:5px">
                    <input class="easyui-combobox" id="sch_Department" style="width:100%" data-options="label:'部门:',valueField:'AutoId',textField:'Name',data:departments">
                </div>
                <div style="margin-bottom:5px">
                    <input class="easyui-combobox" id="sch_Title" style="width:100%" data-options="label:'职等:',valueField:'AutoId',textField:'Name',data:titles">
                </div>
                <div style="text-align:center;padding:5px 0">
                    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="exeSearch()" style="width:80px">搜索</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearSearch()" style="width:80px">重置</a>
                </div>
                <script>
                function exeSearch() {
                    searchArg = [];
                    var restain1 = {'Key':'Code','Op':'Lk','Value':'%'+$('#sch_Code').textbox('getValue')+'%'};
                    var restain2 = {'Key':'Name','Op':'Lk','Value':'%'+$('#sch_Name').textbox('getValue')+'%'};
                    searchArg.push(restain1);
                    searchArg.push(restain2);
                    var sch_rg = $("#sch_Department").combobox("getValue");
                    if(sch_rg>0){
                        searchArg.push({'Key':'DepartmentId','Op':'Eq','Value':sch_rg});
                    }
                    var sch_sp = $("#sch_Title").combobox("getValue");
                    if(sch_sp>0){
                        searchArg.push({'Key':'TitleId','Op':'Eq','Value':sch_sp});
                    }
                    
                    $('#search').window('close');
                    setData();
                }
                function clearSearch() {
                    $('#sch_Code').textbox('setValue',"");
                    $('#sch_Name').textbox('setValue',"");
                    $("#sch_Department").combobox("setValue","");
                    $("#sch_Title").combobox("setValue","");
                }
             </script>
            </div>
        </div>
	</div>
</body>
</html>